package xyz.icxl.android.architecture.helper

import java.util.*
import kotlin.collections.ArrayList

/**
 * 文件描述：模拟图片
 *
 * 文件创建人：cxl
 *
 * 创建时间：2021 年 09月 14 日
 *
 * 文件版本：v1.0
 *
 * 版本描述：创建文件
 */
object MockImageUtils {

    private const val URL = "https://dummyimage.com/"

    enum class FORMAT(val format: String) {
        PNG("png"),
        GIF("gif"),
        JPG("JPG"),
        NONE("")
    }

    /**
     * 随机一个
     */
    fun randomImageUrl(
        size: String = "600x400",
        format: FORMAT = FORMAT.NONE
    ) =
        "$URL$size/${randomColor()}/${randomColor()}${if (format != FORMAT.NONE) "." + format.format else ""}"

    /**
     * 随机一组
     */
    fun randomImageUrlList(
        size: String = "600x400",
        format: FORMAT = FORMAT.NONE, quantity: Int = 4
    ): ArrayList<String> {
        val list = ArrayList<String>()
        for (i in 0..quantity)
            list.add(randomImageUrl(size, format))
        return list
    }


    private fun randomColor(): String {
        val random = Random()
        var r: String = Integer.toHexString(random.nextInt(256))
        var g: String = Integer.toHexString(random.nextInt(256))
        var b: String = Integer.toHexString(random.nextInt(256))
        val sb = StringBuffer()
        //判断获取到的R,G,B值的长度 如果长度等于1 给R,G,B值的前边添0
        r = if (r.length == 1) "0$r" else r
        g = if (g.length == 1) "0$g" else g
        b = if (b.length == 1) "0$b" else b
        sb.append("")
        sb.append(r)
        sb.append(g)
        sb.append(b)
        return sb.toString()
    }
}